package com.moon.cloud.log.util;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

public class LogUtils {
	public static boolean isFileParameter(Object param) {
		return param instanceof MultipartFile || param instanceof File;
	}

	/**
	 * 获取客户端真实IP地址
	 */
	public static String getClientIpAddress(HttpServletRequest request) {
		String xForwardedFor = request.getHeader("X-Forwarded-For");
		if (xForwardedFor != null && !xForwardedFor.isEmpty() && !"unknown".equalsIgnoreCase(xForwardedFor)) {
			// 多次反向代理后会有多个IP值，第一个IP才是真实IP
			return xForwardedFor.split(",")[0].trim();
		}

		String xRealIp = request.getHeader("X-Real-IP");
		if (xRealIp != null && !xRealIp.isEmpty() && !"unknown".equalsIgnoreCase(xRealIp)) {
			return xRealIp;
		}

		return request.getRemoteAddr();
	}

}
